home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
5_7.lha
/
5_7
/
5_7a2.c
< prev
next >
Wrap
Text File
|
1993-08-08
|
1KB
|
59 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
/ Add one to the bin which contains a.
/ Adjust the histogram, if necessary.
include <histogram.h>
include <string.h>
oid histogram::add(int a)
register int i, j;
// make l <= a < r, possibly expanding histogram by
// doubling the binsize and range.
// First the left end.
while (a < l)
{
// combine the bins
for (i = nbin - 1, j = nbin - 2;
0 <= j;
i--, j -= 2)
h[i] = h[j] + h[j + 1];
// check for odd number of bins
if (j == -1)
h[i--] = h[0];
// zero out the rest
if (i >= 0)
memset(h, 0, (i + 1) * sizeof(int));
binsize += binsize;
l -= r - l;
}
// Then the right end
while (r <= a)
{
// combine the bins
int halfbin = nbin / 2;
for (i = 0, j = 0; i < halfbin; i++, j += 2)
h[i] = h[j] + h[j + 1];
// check for odd number of bins
if (j < nbin)
h[i++] = h[j];
// zero out the rest
if (i < nbin)
memset(&h[i], 0, (nbin - i) * sizeof(int));
binsize += binsize;
r += r - l;
}
sum += a;
sqsum += a * a;
h[(a - l) / binsize]++;